import {Page} from '@/base/Page';
import {DeviceMaintainRule} from '@/base/model/DeviceMaintainRule';
import {getDeviceMaintainList, putDeviceMaintain} from '@/base/api/DeviceMaintainApi';
import {deepAssign} from "@/base/util/Objects";
import {getDeviceSort} from "@/base/api/DeviceSortApi";
import {DeviceSort} from "@/base/model/DeviceSort";
import {getDeviceModel} from "@/base/api/DeviceModelApi";
import {DeviceModel} from "@/base/model/DeviceModel";

export class FormBloc {
  model: any = {
    modelId: 0 || undefined,
    modelName: '',
    sortId: 0 || undefined
  };
  paged: Page<DeviceMaintainRule> = new Page<DeviceMaintainRule>([], 0, 1, 15, 0, []);
  sortpage: Page<DeviceSort> = new Page<DeviceSort>([], 0, 1, 10, 0, []);
  modelpage: Page<DeviceModel> = new Page<DeviceModel>([], 0, 1, 10, 0, []);
  sortList: any = [];
  modelList: any = [];

  loading = false;

  load(pageIndex: number, pageSize: number) {
    this.loading = true;
    this.model.modelName = this.getmodelName(this.model.modelId);
    // this.model.sortName = this.getSortName(this.model.sortId);
    // console.log(this.model.sortId);
    return getDeviceMaintainList({
      pageIndex,
      pageSize,
      sortDirection: 'DESC',
      sortId: this.model.sortId,
      modelName: this.model.modelName
    }).then((res) => {
      // console.log(res.data);
      this.paged = res.data;
    }).finally(() => {
      this.loading = false;
    });
  }
  reset() {
    this.model = {
      modelId: 0 || undefined,
      modelName: '',
      sortId: 0 || undefined
    }; // 重置要把表单还原
    this.paged.pageIndex = 1;
    this.paged.pageSize = 15;
    this.load(this.paged.pageIndex, this.paged.pageSize);
    this.getAllSort(1, 1000, 'DESC');
    this.getAllModel(1, 1000, undefined, 'DESC', undefined);
  }
  // removeItem(dateItem: DeviceMaintainRule) {
  //   this.loading = true;
  //   return deleteDeviceMaintain(dateItem.id)
  //     .finally(() => {
  //       this.loading = false;
  //     });
  // }
  getsortName(sortId: number) {
    const item = this.sortList.find(x => x.id === sortId);
    return item == null ? '' : item.sortName;
  }
  getmodelId(modelName: string) {
    const item = this.modelList.find(x => x.modelName === modelName);
    return item == null ? '' : item.id;
  }

  getAllSort(pageIndex: number, pageSize: number, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getDeviceSort({
      pageIndex,
      pageSize,
      sortDirection
    }).then((res) => {
      deepAssign(this.sortpage, res.data);
      this.sortList = this.sortpage.items.map(function (item) {
        var obj = { id: 0, sortName: '' };
        // @ts-ignore
        obj.id = item.id;
        obj.sortName = item.sortName;
        return obj;
      });
      return this.sortList;
    }).finally(() => {
      this.loading = false;
    });
  }

  getSortName(id) {
    let item = this.sortList.find(item => item.id === id);
    return item == null ? '' : item.sortName;
  }
  getmodelName(modelId: number) {
    const item = this.modelList.find(x => x.id === modelId);
    return item == null ? '' : item.modelName;
  }
  getAllModel(pageIndex: number, pageSize: number, sortId, sortDirection: string, companyId) {
    this.loading = true;
    let that = this;
    return getDeviceModel({
      pageIndex,
      pageSize,
      sortId,
      sortDirection,
      companyId
      // id: this.model.id,
      // modelName: this.model.modeName
    }).then((res) => {
      deepAssign(this.modelpage, res.data);
      this.modelList = this.modelpage.items.map(function (item) {
        var obj = { id: 0, modelName: '' };
        obj.id = item.id;
        obj.modelName = item.modelName;
        return obj;
      });
      return this.modelList;
    }).finally(() => {
      this.loading = false;
    });
  }
  modifyItem(
    deviceSortId:number,
    id:number,
    usageTime:number,
    description:string
  ) {
    this.loading = true;
    return putDeviceMaintain({
      id,
      usageTime
    }).then((res) => {
      return res;
    }).finally(() => {
      this.loading = false;
    })
  }
}
